
/**
 * String Comparator compare Strings according to the lexicographical order of Strings.
 */
public class StringComparator implements Comparator {

	@Override
	public int compare(Object o1, Object o2) {
		int ans=666;
		if ((o1 instanceof String) 	&& (o2 instanceof String)
									&& (o1!=null) && (o2!=null))
		{
			String s1 = (String)o1;
			String s2 = (String)o2;
			while (s1.length()>1 && s2.length()>1 && s1.charAt(0)==s2.charAt(0)){
				s1=s1.substring(1);
				s2=s2.substring(1);
			}
			while (ans==666){
				if (s2.length()==0) {
					if (s1.length()==0) 
						ans=0;
					else
						ans=1;
				}
				else if (s1.length()==0) {
					ans=-1;
				}
				else if (s1.charAt(0)>s2.charAt(0)) {
					ans=1;
				}
				else if (s1.charAt(0)<s2.charAt(0)) {
					ans=-1;
				}
				else if (s1.charAt(0)==s2.charAt(0)) {
					if (s1.length()>s2.length())
						ans=-1;
					else if (s1.length()<s2.length())
						ans=1;
					else
						ans=0;
				}
			}
		}
		else 
			throw new RuntimeException("Cannot compare between objects");
		return ans;
	}

}
